package io.renren.modules.newBackground.shanghaiCmcc.v2.example;

import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import javax.net.ssl.SSLContext;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;

public class HttpsToOpenapi {
	
	private static String openapisUrl = "http://211.136.111.133/open/service/accessHttps";
	private static String appCode = null;
	private static String apiCode = null;
	private static String transactionId = null;
	private static String accessToken = null;
	private static String requestBody = null;
	private CloseableHttpClient client;
	
	public static void main(String[] args) {
		try {
			HttpsToOpenapi openapi = new HttpsToOpenapi("D:/primetonProduct/openapi/sdkkey","00000");
			openapi.send();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public HttpsToOpenapi(String keystorePath, String password) throws Exception {
		KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
		FileInputStream instream = new FileInputStream(new File(keystorePath));
		try {
			trustStore.load(instream, password.toCharArray());
		} finally {
			instream.close();
		}

		SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
				SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
		this.client = HttpClients.custom().setSSLSocketFactory(sslsf).build();
		//this.client = HttpClients.createDefault();
	}
	
	
	private void send() throws Exception {
		HttpPost httpPost = null;
		CloseableHttpResponse response = null;
		String tmp = null;
		try {
			httpPost = new HttpPost(openapisUrl);
			httpPost.addHeader("appCode", appCode);
			httpPost.addHeader("apiCode", apiCode);
			httpPost.addHeader("transactionId", transactionId);
			httpPost.addHeader("accessToken", accessToken);
			httpPost.addHeader("sdkVersion", "sdk.version.2.2");
			httpPost.addHeader(HttpHeaders.CONNECTION, "close");
			httpPost.setEntity(new StringEntity(requestBody, ContentType.create("text/plain", "UTF-8")));
			response = client.execute(httpPost);
			InputStreamReader isr = new InputStreamReader(response.getEntity().getContent(),"UTF-8");
			BufferedReader br = new BufferedReader(isr);
			StringBuffer sb = new StringBuffer();
			while ((tmp = br.readLine()) != null) {
				sb.append(tmp);
			}
			String responseBody = sb.toString();
			System.err.println("++++++responseBody:"+responseBody);
		} finally {
			if (response != null) {
				response.close();
			}
			if (httpPost != null) {
				httpPost.abort();
			}
		}
	}

}
